// pages/userLogin/login.js
import Dialog from '../../miniprogram_npm/weapp/dialog/dialog';
const app = getApp()
Page({
  data: {
    username: "",
    password: "",
    userNameerrorMsg: '',
    userPassworderrorMsg: '',
    checkbox: false,
    protectionAgreementUrl:"https://mp.weixin.qq.com/s/PYch4DhfiovqHvdqahYqqg",
    serviceAgreementUrl:"",

  },
  loginBack() {
    wx.navigateBack({
      delta: 1,
    })
  },

  isRead() {
    //弹出框的操作，格外调用组件
    const beforeClose = (action) => new Promise((resolve) => {
      setTimeout(() => {
        if (action === 'confirm') {
          this.setData({
            checkbox: true,
          });
          resolve(true);
        } else {
          // 拦截取消操作
          this.setData({
            checkbox: false,
          });
          resolve(true);
        }
      }, 1000);
    });

    Dialog.confirm({
      title:"服务协议及隐私保护",
      message:"  为了更好地保障你的合法权益，请你阅读并同意一下协议 《鲤鱼科技隐私保护政策》《鲤鱼科技在线服务协议》",
      confirmButtonText:"同意",
      cancelButtonText:"不同意",
      beforeClose
    });
  },

  login() {
    if (this.data.username == '') {
      this.setData({
        userNameerrorMsg: "请输入用户名"
      })
    } else {
      this.setData({
        userNameerrorMsg: ''
      })
    }
    if (this.data.password == '') {
      this.setData({
        userPassworderrorMsg: "密码不能为空"
      })
    } else if (this.data.checkbox == false) {
      this.isRead()
    } else {
      this.setData({
        userPassworderrorMsg: ''
      })
    }
  },
  wxLogin(ee) {
    // console.log(ee.detail.errMsg);
    //console.log(ee.detail.userInfo)
    //console.log(ee.detail.rawData)
    if (ee.detail.errMsg == "getUserProfile:ok") {
      wx.login({
        success(res) {
          app.setGlobalUserInfo(ee.detail.userInfo);
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 3000
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '../user/user',
              success() {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onLoad();
              }
            });
          }, 3000)
          /* //登录的临时凭证
           var code = res.code;
           //调用后端，获取微信的session_key,secret
           wx.request({
             url: 'springBoot的后端地址url',
             method: "POST",
             success: function (result) {
               console.log(result)
               app.setGlobalUserInfo(ee.detail.userInfo);
               wx.switchTab({
                 url: '../user/user',
               })
             }
           }) */
        },
      })
    } else {
      wx.showToast({
        title: '登录失败',
        icon: 'error',
        duration: 2000
      })
    }

  },
  /**
   * 单选按钮事件
   */
  onChange(event) {
    console.log(event)
    this.setData({
      checkbox: event.detail,
    });
  },

/**
 * 保护政策跳转
 */
protectionAgreement(){
  wx.navigateTo({
    url: '../../Components/webComponent/web-view?url='+this.data.protectionAgreementUrl,
  })
},

/**
 * 服务协议跳转
 */
serviceAgreement(){
  wx.navigateTo({
    url: '../../Components/webComponent/web-view?url='+this.data.serviceAgreementUrl,
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})